import 'package:flutter/material.dart';
import 'package:top_foundation/top_foundation.dart';
import 'package:top_foundation/utils/theme/theme.dart';

import 'index.dart';

class DialogPage extends GetView<DialogController> {
  const DialogPage({Key? key}) : super(key: key);

  void sureEvent() async {
    Get.back(result: 1);
  }

  void dismiss() async {
    Get.back(result: 0);
  }

  Widget _buildContent(Widget child) {
    return Container(
      constraints: BoxConstraints(maxHeight: 0.45.sh),
      child: SingleChildScrollView(
        child: child,
      ),
    );
  }

  // 主视图
  Widget _buildView() {
    var m = controller.state.m;
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: [
        Card(
          clipBehavior: Clip.hardEdge,
          margin: m.margin ?? EdgeInsets.symmetric(horizontal: 25.r),
          child: Container(
            child: Column(
              children: [
                if (m.title != null)
                  Container(
                    padding: EdgeInsets.symmetric(
                        vertical:
                            (m.msg == null && m.content == null) ? 30 : 15),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        border: m.hasHeaderLine &&
                                (m.msg != null || m.content != null)
                            ? Border(bottom: ITheme.borderSide)
                            : null),
                    child: Text(
                      m.title!,
                      maxLines: 1,
                      style: ITheme.text.headlineLarge,
                    ),
                  ),
                _buildContent(
                  Column(
                    children: [
                      if (m.msg != null)
                        Container(
                          padding: const EdgeInsets.symmetric(
                              vertical: 30, horizontal: 15),
                          child: Text(
                            m.msg!,
                            textAlign: TextAlign.left,
                            style: ITheme.text.subtitle15,
                          ),
                        ),
                      if (m.content != null) m.content!,
                    ],
                  ),
                ),

                // if (m.msg == null && m.content == null)
                //   const SizedBox(
                //     height: 10,
                //   ),
                if (m.sureText != null || m.cancelText != null)
                  BtnBar(
                    onTap: (idx) {
                      if (idx == 0) {
                        //取消
                        dismiss();
                      } else {
                        //确定
                        if (m.conditionDismiss != null) {
                          if (m.conditionDismiss!()) {
                            sureEvent();
                          }
                        } else {
                          sureEvent();
                        }
                      }
                    },
                    left: m.cancelText,
                    right: m.sureText,
                  ),
              ],
            ),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DialogController>(
      builder: (_) {
        return GestureDetector(
          child: Container(
            color: Colors.black54,
            child: SafeArea(
              child: _buildView(),
            ),
          ),
        );
      },
    );
  }
}
